﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace DataAccess
{
    public partial class DataAccess
    {
        public DataAccessResult GetListHostServices(String SubscriptionID, String CertificatePath)
        {
            DataAccessResult result = new DataAccessResult();
            string requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/services/hostedservices";
            
            string ReturnBody = string.Empty;
            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);

                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "GET";
                request.ClientCertificates.Add(cert);
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }
                
                resp = request.GetResponse();
              
                if (resp != null)
                {
                    
                    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), true))
                    {
                        ReturnBody = sr.ReadToEnd();
                        sr.Close();
                    }
                    if (ReturnBody != null)
                    {
                        List<Entities.HostedService>  HostServiceList = new List<Entities.HostedService>();
                        XElement el = XElement.Parse(ReturnBody);
                        IEnumerable<XElement> allElements = el.Elements();
                        foreach (XElement e1 in allElements)
                        {
                            Entities.HostedService hs = new Entities.HostedService();
                            foreach (XElement element in e1.Elements())
                            {
                                string ElementName = element.Name.LocalName.Trim().ToLower();
                                string ElementValue = element.Value;

                                switch (ElementName)
                                {
                                    case "url":
                                        hs.Url = ElementValue;
                                        break;
                                    case "servicename":
                                        hs.ServiceName = ElementValue;
                                        break;
                                }
                            }
                            HostServiceList.Add(hs);
                        }
                        result.IsOperationSuccessful = true;
                        result.HostServiceList = HostServiceList;
                    }
                }
            }
            catch (WebException webEx)
            {
                result.Error = webEx;
                result.HostServiceList = null;
                result.IsOperationSuccessful = false;
            }
            catch (Exception excep)
            {
                result.Error = excep;
                result.HostServiceList = null;
                result.IsOperationSuccessful = false;
            }
            finally
            {
                if (resp != null) { resp.Close(); }
                resp = null;
            }
            return result;           
        }

    }
}
